/**
 * AST节点的类型。
 */
 const ASTNodeType = {
  Program: 'Program',           //程序入口，根节点

  IntDeclaration: 'IntDeclaration',     //整型变量声明
  ExpressionStmt: 'ExpressionStmt',     //表达式语句，即表达式后面跟个分号
  AssignmentStmt: 'AssignmentStmt',     //赋值语句

  Primary: 'Primary',            //基础表达式
  Multiplicative: 'Multiplicative',     //乘法表达式
  Additive: 'Additive',           //加法表达式

  Identifier: 'Identifier',         //标识符
  IntLiteral: 'IntLiteral'          //整型字面量
}

class SimpleAstNode {
  parent // 父节点

  /**
   * 
   * @param nodeType // 节点类型 
   * @param text // 节点文本
   */
  constructor(nodeType, text) {
    this.children = [] // 子节点集合
    this.nodeType = nodeType
    this.text = text
  }

  getParent() {
    return this.parent
  }

  getChildren() {
    return this.children
  }

  getText() {
    return this.text
  }

  addChild(child) {
    this.children.push(child)
    child.parent = this
  }
}

/**
 * 
 * @param node // 节点 
 * @param indent // 缩进字符
 */
const printAst = (node, indent = '') => {
  console.log(indent + node.nodeType + ' ' + node.text)
  const children = node.getChildren()
  children.forEach(child => {
    printAst(child, indent + '\t')
  })
}

module.exports = {
  ASTNodeType,
  SimpleAstNode,
  printAst
}